<?php
include_once('./config/config.php');
if ($_POST) {
    // 获取行为操作
    $action = $_POST['action'];
    // 结果集
    $result = [
        'code' => 0,
        'msg' => '未知信息',
        'data' => null
    ];
    // 删除管理员
    if ($action == 'delete') {
        $id = $_POST['id'];
        $sql = "SELECT * FROM `pre_admin` WHERE `id` = '$id'";
        $admin = find($sql);
        if (!$admin) {
            $result['msg'] = '用户不存在';
        }
        $res = delete('admin', "`id`='$id'");
        if ($res['code'] === 1) {
            if ($admin && $admin['avatar']) {
                is_file($admin['avatar']) && @unlink($admin['avatar']);
            }
            $result['code'] = 1;
            $result['msg'] = '删除成功';
        } else {
            // echo $res['msg'] ;
            $result['msg'] = '删除失败';
        }
    }
    // 删除部门
    if ($action == 'depdelete') {
        $id = $_POST['id'];
        $sql = "SELECT * FROM `pre_department` WHERE `id` = '$id'";
        $admin = find($sql);
        if (!$admin) {
            $result['msg'] = '部门不存在';
        }
        $res = delete('department', "`id`='$id'");
        if ($res['code'] === 1) {
            $result['code'] = 1;
            $result['msg'] = '删除成功';
        } else {
            // echo $res['msg'] ;
            $result['msg'] = '删除失败';
        }
    }
    // 删除职位
    if ($action == 'jobdelete') {
        $id = $_POST['id'];
        $sql = "SELECT * FROM `pre_job` WHERE `id` = '$id'";
        $admin = find($sql);
        if (!$admin) {
            $result['msg'] = '职位不存在';
        }
        $res = delete('job', "`id`='$id'");
        if ($res['code'] === 1) {
            $result['code'] = 1;
            $result['msg'] = '删除成功';
        } else {
            // echo $res['msg'] ;
            $result['msg'] = '删除失败';
        }
    }
    // 删除员工
    if ($action == 'Perdelete') {
        $id = $_POST['id'];
        $sql = "SELECT * FROM `pre_person` WHERE `id` = '$id'";
        $admin = find($sql);
        if (!$admin) {
            $result['msg'] = '员工不存在';
        }
        $res = delete('person', "`id`='$id'");
        if ($res['code'] === 1) {
            if ($admin && $admin['avatar']) {
                is_file($admin['avatar']) && @unlink($admin['avatar']);
            }
            $result['code'] = 1;
            $result['msg'] = '删除成功';
        } else {
            // echo $res['msg'] ;
            $result['msg'] = '删除失败';
        }
    }
    // 查询所有职位(员工编辑)
    if ($action == 'JobSelect') {
        $depid = $_POST['id'];
        $sql = "SELECT * FROM `pre_job` WHERE `dep_id` = '$depid'";
        $JobData = all($sql);
        if (!$JobData) {
            $result['msg'] = '暂无职位';
        } else {
            $result = [
                'msg' => '查询成功',
                'code' => 1,
                'data' => $JobData
            ];
        }
    }
    // 查询部门名称是否存在（部门编辑，部门新增）
    if($action == 'DepSelect') {
        $name=$_POST['name'];
        $sql = "SELECT * FROM `pre_department` WHERE `name`='$name'";
        $DepData = find($sql);
        if ($DepData) {
            $result = [
                'msg' => '该部门已存在！',
                'code' => 1,
                'data' => $DepData
            ];
        }
    }
    // 查询职位名称是否存在(职位编辑)
    if($action == 'Jobselect') {
        $name=$_POST['name'];
        $sql = "SELECT * FROM `pre_job` WHERE `name`='$name'";
        $JobData = find($sql);
        if ($JobData) {
            $result = [
                'msg' => '该职位已存在！',
                'code' => 1,
                'data' => $JobData
            ];
        }
    }
    // 前端期望返回的数据类型json 把数组转成json数据 
    echo json_encode($result);
    exit;
}
